package com.example.demo2.controller;

import com.example.demo2.dto.SampleDTO;
import com.example.demo2.entity.Sample;
import com.example.demo2.entity.SampleOrders;
import com.example.demo2.entity.Standard;
import com.example.demo2.mapper.SampleMapper;
import com.example.demo2.mapper.SpoMapper;
import com.example.demo2.mapper.StandardMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import java.util.List;

@RestController
@CrossOrigin
public class SampleController {
    @Autowired
    SpoMapper spoMapper;
    @Autowired
    SampleMapper sampleMapper;
    @Autowired
    StandardMapper standardMapper;

    private ConcurrentHashMap<String, Integer> concurrentMap;

    @Autowired
    public void MyController(ConcurrentHashMap<String, Integer> concurrentMap) {
        this.concurrentMap = concurrentMap;
    }

    //检测人员所有样品清单
    @GetMapping("/findSpoByUID")
    public List<SampleOrders> findSpoByUID() {
        Integer uID = (Integer) concurrentMap.get("uID");
        List<SampleOrders> sampleOrderss = spoMapper.findSpoByUID(uID);
        for(SampleOrders sampleOrders : sampleOrderss) {
            Integer spId=sampleOrders.getSpId();
            Sample sample = sampleMapper.findSampleBySpId(spId);
            if(sample!=null) {
                sampleOrders.setSpFactory(sample.getSpFactory());
                Standard standard = standardMapper.findStandardByStaId(sample.getStaId());
                sampleOrders.setSpName(sample.getSpName());
                if(standard!=null) {
                    sampleOrders.setStaName(standard.getStaName());
                    sampleOrders.setStaGB(standard.getStaGB());
                }
            }
        }
        return sampleOrderss;
    }

    @GetMapping("/findSpoNumByUID")
    public int findSpoNumByUID() {
        Integer uID = (Integer) concurrentMap.get("uID");
        List<SampleOrders> sampleOrderss = spoMapper.findSpoByUID(uID);
        return sampleOrderss.size();
    }

    //修改样品清单
    @PutMapping("/updateSpo")
    public String updateSpo(@RequestBody Map<String,String> info) {
        Integer spId = Integer.parseInt(info.get("spId"));
        String spoNeed = info.get("spoNeed");
        String spoGg = info.get("spoGg");
        boolean x=true,y=true;
        if(spoGg!=null && !spoGg.equals(""))
        {
            x=spoMapper.updateSpoGg(spId,spoGg);
        }
        if(spoNeed!=null && !spoNeed.equals(""))
        {
            y=spoMapper.updateSpoNeed(spId,spoNeed);
        }
        if(x&y) {
            return "修改成功";
        }
        else{
            return "修改失败";
        }
    }

    //删除样品清单
    @PutMapping("/deleteSpo/{spoId}")
    public int deleteSpo(@PathVariable int spoId) {
        return spoMapper.deleteSpo(spoId);
    }

    @GetMapping("/sampleList")
    public List<Sample> sampleList(int staId){
        return sampleMapper.FindAllSample(staId);
    }

    @PutMapping("/AddSampleOrder")
    public String AddSampleOrder(@RequestBody SampleOrders sample){
        System.out.println("sample中的值为："+sample.getSpName());
        Integer uID = (Integer) concurrentMap.get("uID");
        int fl = sampleMapper.AddSampleorders(uID,sample.getSpId(),sample.getSpoNeed(),"未发放", sample.getSpoGg());
        if(fl == 1)
            return "添加成功";
        else return "添加失败";
    }

    @GetMapping("/findSample/{spId}")
    public Sample findSample(@PathVariable int spId) {
        return sampleMapper.findSampleBySpId(spId);
    }



    @GetMapping("/getSamples")
    public ResponseEntity<List<SampleDTO>> getSamplesByStaId(@RequestParam("staId") int staId) {
        List<SampleDTO> samples = sampleMapper.getSamples(staId);
        return ResponseEntity.ok(samples);
    }


    @PostMapping("/addSample")
    public ResponseEntity<SampleDTO> addSample(@RequestBody SampleDTO sample) {
        int result = sampleMapper.addSample(sample);
        if (result > 0) {
            return ResponseEntity.ok(sample);
        } else {
            return ResponseEntity.status(500).build();
        }
    }


    @PutMapping("/apdateSample/{spId}")
    public ResponseEntity<String> updateSample(@PathVariable("spId") int spId, @RequestBody SampleDTO sample) {
        sample.setSpId(spId);
        int result = sampleMapper.updateSampleBySpId(sample);
        if (result > 0) {
            return ResponseEntity.ok("Sample updated successfully.");
        } else {
            return ResponseEntity.status(404).body("Sample not found.");
        }
    }


    @DeleteMapping("deleteSample/{spId}")
    public ResponseEntity<String> deleteSample(@PathVariable("spId") int spId) {
        int result = sampleMapper.deleteSampleBySpId(spId);
        if (result > 0) {
            return ResponseEntity.ok("Sample deleted successfully.");
        } else {
            return ResponseEntity.status(404).body("Sample not found.");
        }
    }
}
